class SnakeGame {
    constructor(canvas, updateScore) {
        this.canvas = canvas;
        this.ctx = canvas.getContext('2d');
        this.updateScore = updateScore;
        
        // 游戏配置
        this.tileSize = 20;
        this.tileCount = 20;
        
        // 初始化游戏状态
        this.reset();
        
        // 绑定键盘事件
        this.handleKeyPress = this.handleKeyPress.bind(this);
        document.addEventListener('keydown', this.handleKeyPress);
    }

    reset() {
        // 蛇的初始位置和方向
        this.snake = [{x: 10, y: 10}];
        this.velocity = {x: 0, y: 0};
        this.food = this.generateFood();
        this.score = 0;
        this.gameOver = false;
        this.started = false;
        this.updateScore(this.score);
    }

    generateFood() {
        const position = {
            x: Math.floor(Math.random() * this.tileCount),
            y: Math.floor(Math.random() * this.tileCount)
        };
        
        // 确保食物不会出现在蛇身上
        while (this.snake.some(segment => segment.x === position.x && segment.y === position.y)) {
            position.x = Math.floor(Math.random() * this.tileCount);
            position.y = Math.floor(Math.random() * this.tileCount);
        }
        
        return position;
    }

    handleKeyPress(event) {
        if (!this.started && event.key !== 'Enter') {
            return;
        }

        switch(event.key) {
            case 'ArrowUp':
                if (this.velocity.y !== 1) {
                    this.velocity = {x: 0, y: -1};
                    this.started = true;
                }
                break;
            case 'ArrowDown':
                if (this.velocity.y !== -1) {
                    this.velocity = {x: 0, y: 1};
                    this.started = true;
                }
                break;
            case 'ArrowLeft':
                if (this.velocity.x !== 1) {
                    this.velocity = {x: -1, y: 0};
                    this.started = true;
                }
                break;
            case 'ArrowRight':
                if (this.velocity.x !== -1) {
                    this.velocity = {x: 1, y: 0};
                    this.started = true;
                }
                break;
            case 'Enter':
                if (this.gameOver) {
                    this.reset();
                }
                break;
        }
    }

    update() {
        if (this.gameOver || !this.started) {
            return;
        }

        // 移动蛇
        const head = {
            x: (this.snake[0].x + this.velocity.x + this.tileCount) % this.tileCount,
            y: (this.snake[0].y + this.velocity.y + this.tileCount) % this.tileCount
        };

        // 检查是否撞到自己
        if (this.snake.some(segment => segment.x === head.x && segment.y === head.y)) {
            this.gameOver = true;
            return;
        }

        this.snake.unshift(head);

        // 检查是否吃到食物
        if (head.x === this.food.x && head.y === this.food.y) {
            this.score += 10;
            this.updateScore(this.score);
            this.food = this.generateFood();
        } else {
            this.snake.pop();
        }
    }

    draw() {
        // 清空画布
        this.ctx.fillStyle = '#1E1B2E';
        this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);

        // 绘制食物
        this.ctx.fillStyle = '#00FF88';
        this.ctx.fillRect(
            this.food.x * this.tileSize,
            this.food.y * this.tileSize,
            this.tileSize - 2,
            this.tileSize - 2
        );

        // 绘制蛇
        this.ctx.fillStyle = '#6C5CE7';
        this.snake.forEach((segment, index) => {
            this.ctx.fillRect(
                segment.x * this.tileSize,
                segment.y * this.tileSize,
                this.tileSize - 2,
                this.tileSize - 2
            );
        });

        // 绘制游戏开始/结束提示
        if (!this.started || this.gameOver) {
            this.ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
            this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
            
            this.ctx.fillStyle = '#FFFFFF';
            this.ctx.font = '20px "Noto Sans SC"';
            this.ctx.textAlign = 'center';
            this.ctx.textBaseline = 'middle';
            
            if (this.gameOver) {
                this.ctx.fillText('游戏结束！按回车键重新开始', this.canvas.width / 2, this.canvas.height / 2);
            } else {
                this.ctx.fillText('使用方向键开始游戏', this.canvas.width / 2, this.canvas.height / 2);
            }
        }
    }
}
